// Creating details view

var detailsAddPost = new DetailsView();
var detailsLogin = new DetailsView();
var detailsChooseLayout = new DetailsView();
var detailsEditPost = new DetailsView();
var detailsEditProj = new DetailsView();

//--------------------------------------------------------------------------------------------------

/**
*	Request for calling add post details view.
*/
function showAddPost(container) {
    detailsAddPost.SetContent("",undefined,"details_addPost.xml",false,0);
    detailsAddPost.detailsViewData.putValue("title", DETAILS_NEWPOST); // TODO put on string.xml
    detailsAddPost.detailsViewData.putValue("container", container);
    
    detailsAddPost.detailsViewData.putValue("task", "");
    detailsAddPost.detailsViewData.putValue("description", "");
    detailsAddPost.detailsViewData.putValue("stars", 0);

	detailsEditPost.detailsViewData.putValue("expires", false);
    
    plugin.showDetailsView(detailsAddPost, "", gddDetailsViewFlagNone, onDetailsAddPostFeedback);
    
}

function onDetailsAddPostFeedback(detailsViewFlags) {
    if (detailsAddPost.detailsViewData.getValue("confirmed")) {
        var container = detailsAddPost.detailsViewData.getValue("container");
        var task = detailsAddPost.detailsViewData.getValue("task");
        var description = detailsAddPost.detailsViewData.getValue("description");
        var day = detailsAddPost.detailsViewData.getValue("day");
        var month = detailsAddPost.detailsViewData.getValue("month");
        var year = detailsAddPost.detailsViewData.getValue("year");
        if (task=="") { alert(NO_TASK_CHOSEN); }
        else {
            var expirationDate = null;
            if (day!="" || month!="" || year!="") {
                expirationDate = detailsAddPost.detailsViewData.getValue("expirationDate");
            }
            if (expirationDate==false) { 
            	alert(INVALID_DATE); 
            } else {
                var newPostIt = new PostIt( user.nextId(), task, description, 0 );
                if (expirationDate) { newPostIt.setExpirationDate( expirationDate ); }
                var project = user.getProject();
                project.addPostIt( newPostIt, container ); // CHECK IF THIS METHOD IS ADDING CORRECTLY
                updateGui( user.getProject() );
                
            }
        }
    }
}


//--------------------------------------------------------------------------------------------------

/**
*	Shows choose layout box.
*/
function showChooseLayout(field){
	 detailsChooseLayout.detailsViewData.putValue("layout", options.getValue(field));
	 detailsChooseLayout.detailsViewData.putValue("field", field);	
	 detailsChooseLayout.SetContent("",undefined,"details_chooseLayout.xml",false,0);
     plugin.showDetailsView(detailsChooseLayout, "", gddDetailsViewFlagNone, onDetailsChooseLayoutFeedback);
}

function onDetailsChooseLayoutFeedback(detailsViewFlags){
	if(detailsChooseLayout.detailsViewData.getValue("confirmed")){
		var field = detailsChooseLayout.detailsViewData.getValue("field");
		var layout = detailsChooseLayout.detailsViewData.getValue("layout");
		options.putValue(field, layout);
                for (var i = 0; colors.opt[i]; i++) colors.opt[i].update();
                updateGui( user.getProject() );
	}
}

//--------------------------------------------------------------------------------------------------

/**
*	Request for calling edit post details view.
*/
function showEditPost(postIt, container){
	detailsEditPost.SetContent("",undefined, "details_addPost.xml", false, 0);
    detailsEditPost.detailsViewData.putValue("container", container);
    detailsEditPost.detailsViewData.putValue("title", DETAILS_EDITPOST);
    detailsEditPost.detailsViewData.putValue("postId", postIt.getId());
    
    detailsEditPost.detailsViewData.putValue("postId", postIt.getId());
    detailsEditPost.detailsViewData.putValue("task", postIt.getTask());
    detailsEditPost.detailsViewData.putValue("description", postIt.getDescription());
    detailsEditPost.detailsViewData.putValue("stars", postIt.getStars());
    
    
    if(postIt.expires()){
		var expiration = postIt.getExpirationDate();
		detailsEditPost.detailsViewData.putValue("day", expiration.getDate() );
		detailsEditPost.detailsViewData.putValue("month", expiration.getMonth() + 1);
		detailsEditPost.detailsViewData.putValue("year", expiration.getFullYear());    
    
		detailsEditPost.detailsViewData.putValue("expires", true);
    } else {
	    detailsEditPost.detailsViewData.putValue("expires", false);
    }
    
    plugin.showDetailsView(detailsEditPost, "", gddDetailsViewFlagNone, onDetailsEditPostFeedback);
}

function onDetailsEditPostFeedback(detailsViewFlags){
    if (detailsEditPost.detailsViewData.getValue("confirmed")) {
        var container = detailsEditPost.detailsViewData.getValue("container");
        var id = detailsEditPost.detailsViewData.getValue("postId");
        var task = detailsEditPost.detailsViewData.getValue("task");
        var description = detailsEditPost.detailsViewData.getValue("description");
        var stars = detailsEditPost.detailsViewData.getValue("stars");
        var day = detailsEditPost.detailsViewData.getValue("day");
        var month = detailsEditPost.detailsViewData.getValue("month");
        var year = detailsEditPost.detailsViewData.getValue("year");
        if (task=="") { alert(NO_TASK_CHOSEN); }
        else {
            var expirationDate = null;
            if (day!="" || month!="" || year!="") {
                expirationDate = detailsEditPost.detailsViewData.getValue("expirationDate");
            }
            if (expirationDate==false) { 
            	alert(INVALID_DATE); 
            } else {
                var newPostIt = new PostIt( id, task, description, stars);
                if (expirationDate) { 
                	newPostIt.setExpirationDate( expirationDate ); 
                }
                var project = user.getProject();
                project.alterPostIt( newPostIt, container ); // CHECK IF THIS METHOD IS ADDING CORRECTLY
                updateGui( user.getProject() );
            }
        }
    }
}

// ---------------

/**
*	Request for calling edit project details view.
*/
function showEditProj() {
    var finalizationDate = user.getProject().getFinalizationDate();
    
    detailsEditProj.SetContent("",undefined,"details_project.xml", false, 0);
    detailsEditProj.detailsViewData.putValue("name", user.getProject().getName());
    detailsEditProj.detailsViewData.putValue("day", finalizationDate.getDate());
    detailsEditProj.detailsViewData.putValue("month", finalizationDate.getMonth()+1);
    detailsEditProj.detailsViewData.putValue("year", finalizationDate.getFullYear());

	plugin.showDetailsView(detailsEditProj, "", gddDetailsViewFlagNone, onDetailsEditProjFeedback);    
}

function onDetailsEditProjFeedback( detailsViewFlags ) {
    if (detailsEditProj.detailsViewData.getValue("confirmed")) {
        var name = detailsEditProj.detailsViewData.getValue("name");
        var finalDate = detailsEditProj.detailsViewData.getValue("finalDate");
        // the next line is specially important (?)
        var finalYear = finalDate.getFullYear();
        if (finalDate==false) {
            alert(INVALID_DATE);
        } else {
            var project = user.getProject();
            project.setName(name);
            project.setFinalizationDate(finalDate);
            updateGui(project);
        }
    }
}

